<template>
  <view class="content">
    <view class="loginBox">
      <h3>感谢支持昊昊</h3>
      <view class="inputBox">
        <view class="ipt">
          <uni-icons type="contact" size="24" color="rgb(247,120,172)"></uni-icons>
          <input type="nickname"  v-model="uname" placeholder="点击获取微信昵称"/>
        </view>
        <!-- <view class="ipt">
          <uni-icons type="eye" size="24" color="rgb(247,120,172)"></uni-icons>
          <input type="passsword" value="" placeholder="请输入密码"/>
        </view> -->
        <view class="forgetPwd">
         
        </view>
        <button @click="wxlogin">登录</button>
      </view>
      <view class="tipbox">
        <view class="txt">
         
        </view>
        <view class="otherUser">
          
        </view>
      </view>
    </view>
    
    <view class="tip">
     昊昊story
    </view>
  </view>
</template>


<script>
	import * as utils from '../../../util/util.js';
	export default {
		data() {
			return {
				uname: "",
				handle:false
			}
		},
		onLoad() {
			//自動登录 记录过openid的直接登录
			var _this = this;
			uni.showLoading({
				title: '微信一键登录中'
			});
			uni.login({
					provider: 'weixin',
					success(wx) {
						uniCloud.callFunction({
							name:"login",
							data: {
								code: wx.code
							},
							success: (res) => {
								if(res.result.status!="200"){
									_this.handle = false;
									utils.toastwarn("获取微信信息失败"+res.result);
									uni.hideLoading();
									return;
								}
								//utils.toastwarn(res.result.data.openid);
								uniCloud.callFunction({
									name:"getUser",
									data:{
										openid:res.result.data.openid
									}
								}).then(function(result){
									console.log(result);
									if(result.result.data.length>=1){
										uni.setStorageSync('token', res.result.data.openid);
										uni.setStorageSync('username', result.result.data[0].name);
										uni.reLaunch({
											url: '/pages/index/index'
										})
										uni.hideLoading();
									}else{
										uni.hideLoading();
										utils.toastwarn("请完善信息后登录");
									}
									
									      
								}).catch(er=>{
									uni.hideLoading();
									utils.toastwarn("请完善信息后登录");
									
								})	
							},
							fail: (res) => {
								_this.handle = false;
								uni.hideLoading();
								utils.toastwarn(res);
							}
						})
			
					},
					fail: (res) => {
						_this.handle = false;
						console.log(res);
					}
			
			})

		},
		methods: {
			wxlogin() { 
				if (this.uname.length == 0) {
					utils.toastwarn("请输入昵称");
					return;
				}
				if(this.handle){
					return;
				}
				uni.showLoading({
					title: '微信一键登录中'
				});
				this.handle = true;
				var _this = this;
				uni.login({
						provider: 'weixin',
						success(wx) {
							uniCloud.callFunction({
								name:"login",
								data: {
									code: wx.code
								},
								success: (res) => {
									if(res.result.status!="200"){
										_this.handle = false;
										utils.toastwarn("获取微信信息失败"+res.result);
										return;
									}
									utils.addorupdate({
										name: _this.uname,
										openid: res.result.data.openid
									}).then(function(result) {
											uni.setStorageSync('token', res.result.data.openid);
											uni.setStorageSync('username', _this.uname);
											uni.reLaunch({
												url: '/pages/index/index'
											})
											uni.hideLoading();
										
										
									}).catch(er => {
										uni.hideLoading();
										_this.handle = false;
										utils.toastwarn(er);
									})
								},
								fail: (res) => {
									uni.hideLoading();
									_this.handle = false;
									utils.toastwarn(res);
								}
							})

						},
						fail: (res) => {
							uni.hideLoading();
							_this.handle = false;
							console.log(res);
						}

				})
			}

		}


	}
</script>

<style scoped>
  .content{
    height: 100vh;
    background: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202005%2F10%2F20200510005139_JR8fL.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1714820289&t=e835cde99a094cbd98f9c318f25160ec") no-repeat;
    background-size: 100% 100%;
  }
  .loginBox{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-60%);
    width: 90%;
    background-color: #fff;
    border-radius: 20rpx;
    padding: 60rpx;
    box-sizing: border-box;
  }
  h3{
    color: rgb(247,120,172);
    font-size: 40rpx;
    letter-spacing: 10rpx;
    margin-bottom: 40rpx;
  }
  .inputBox{
    
  }
  .ipt{
    height: 86rpx;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-bottom: 20rpx;
    background-color: #f5f5f5;
    border-radius: 10rpx;
    padding-left: 10rpx;
  }
  .ipt input{
    margin-left: 20rpx;
    font-size: 28rpx;
  }
  .ipt input{
    margin-left: 20rpx;
  }
  .forgetPwd{
    font-size: 26rpx;
    color: #b5b5b5;
    text-align: end;
    padding:0 10rpx;
    display: flex;
    justify-content: space-between;
  }
  button{
    margin-top: 20rpx;
    line-height: 85rpx;
    text-align: center;
    background: linear-gradient(to right,rgb(255, 170, 127),rgb(247,120,172));
    border-radius: 40rpx;
    color: #fff;
    margin-top: 40rpx;
  }
  
  .tip{
    text-align: center;
    font-size: 28rpx;
    position: fixed;
    bottom: 50rpx;
    left: 50%;
    transform: translate(-50%,-50%);
    color: #f4f4f4;
  }
  .tipbox {
    text-align: center;
    margin-top: 100rpx;
  }
  
  .otherUser {
    margin-top: 30rpx;
    display: flex;
    justify-content: center;
  }
  
  .txt {
    font-size: 28rpx;
    color: #cbcbcb;
  }
  
  .otherUser .uni-icons {
    margin-left: 20rpx;
  }
</style>
